package com.bootdo.system.service.impl;

import com.bootdo.common.config.Constant;
import com.bootdo.system.dao.BeautyDeptDao;
import com.bootdo.system.domain.BeautyDeptDO;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.service.BeautyDeptService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service
public class BeautyDeptServiceImpl implements BeautyDeptService {
	@Autowired
	private BeautyDeptDao beautyDeptDao;
	
	@Override
	public BeautyDeptDO get(Long deptId){
		return beautyDeptDao.get(deptId);
	}
	
	@Override
	public List<BeautyDeptDO> list(Map<String, Object> map){
		return beautyDeptDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return beautyDeptDao.count(map);
	}
	
	@Override
	public int save(BeautyDeptDO beautyDept){
		return beautyDeptDao.save(beautyDept);
	}
	
	@Override
	public int update(BeautyDeptDO beautyDept){
		return beautyDeptDao.update(beautyDept);
	}
	
	@Override
	public int remove(Long deptId){
		return beautyDeptDao.remove(deptId);
	}
	
	@Override
	public int batchRemove(Long[] deptIds){
		return beautyDeptDao.batchRemove(deptIds);
	}

	@Override
	public List<DeptDO> beautyClassList(Map<String, Object> params) {
		long count= calCount(params);
		if(count == 0) {
			return new ArrayList<DeptDO>();
		}else {
			int index = (int) count - 1;
			//params.put("count", count);
			List<DeptDO> list = beautyDeptDao.beautyClassList(params);
			int newIndex = 0;
			if(!CollectionUtils.isEmpty(list)) {
				newIndex = getIndex(index,list);
			}
			return list.subList(0,newIndex + 1);
		}
	}

	private int getIndex(int index, List<DeptDO> list) {
		if(list.get(index).getScore() == list.get(index + 1).getScore()) {
			index = getIndex(index+1,list);
		}
		return index;
	}

	private int calCount(Map<String, Object> map) {
		int count = beautyDeptDao.count(map);
		//DictDO dictDO = DictUtils.getDictByType("beauty_dept");
		BigDecimal rate = new BigDecimal(Constant.BEAUTY_DEPT);
		/*if (dictDO != null) {
			rate = new BigDecimal(dictDO.getValue());
		}*/
		BigDecimal total = new BigDecimal(count).multiply(rate);
		Long number = Math.round(total.doubleValue());
		return number.intValue();
	}

}
